/**
 * Created by 白雨浓 on 17-10-19 下午1:15.
 *
 * localStorage和sessionStorage工具
 */
const Storage = {
    install(Vue){

        const local = {
            setItem(k, v){
                localStorage.setItem(k, JSON.stringify(v));
            },
            getItem(k){
                const v = localStorage.getItem(k);
                if (v) return JSON.parse(v);
                else return null;
            },
            removeItem(k){
                const v = this.getItem(k);
                localStorage.removeItem(k);
                return v;
            },
            clear(){
                localStorage.clear();
            }
        };

        const session = {
            setItem(k, v){
                sessionStorage.setItem(k, JSON.stringify(v));
            },
            getItem(k){
                const v = sessionStorage.getItem(k);
                if (v) return JSON.parse(v);
                else return null;
            },
            removeItem(k){
                const v = this.getItem(k);
                sessionStorage.removeItem(k);
                return v;
            },
            clear(){
                sessionStorage.clear();
            }
        };

        Vue.prototype.$storage = {local, session}
    }
};

export default Storage;


